const App = {
  data() {
    return {
      totalMembers: 111, //总人数
      fullMembers: 11, //正式党员
      probationaryMembers: 1, //预备党员
      input: "", //输入框

      treeData: [], //树的数据
      originalTreeData: [], //原始树的数据
      originalTableData: [], // 原始表格的数据
      columns: [], //表格的列
      loading: false, //表数据的懒加载
      showDialog: false, //设置字段弹窗
      checkedFields: [], //选中的字段
      isDataReady: false,
      Tabletdata: [], //表格的数据
      fields: [], //字段列表
      // showaddDialog: false, //添加字段弹窗
      value: "",
      Total: "",
      currentPage: 1, //当前页码
      pageSize: 1, // 每页记录数
    };
  },
  mounted() {
    this.Treedata();
    this.getList();
  },
  computed: {
    // 计算属性，返回选中的字段
    isAllChecked() {
      return this.checkedFields.length === this.fields.length;
    },
  },
  methods: {
    // 树删除
    remove(node, data) {
      console.log('删除的节点', data);
      console.log(data);
      data.Row_Status = "1"
      // 这里添加表单提交逻辑
      let A = new Array()  //创建一个数组A
      A.push(data)    //将form表单添加到A中
      console.log(A)
      let B = new Object() //创建一个对象B
      B.grid_information = A  //将A赋给B的grid_information（名字可以自定义）属性
      console.log(B)
      let C = new Object()  //创建一个对象C
      C.Modify = B             //将B赋给C的Add属性
      console.log('删除的当大', C)
      const _this = this
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: "https://service.essp.top/apiForMoHe/generalBusiness/generalQueryByTableNameWithOriginal",
        method: "post",
        data: {
          Data: "grid_information", //表名
          Condition: "Grid_SerialNumber ='" + data.ProvinceNumber + "'",
        },
        success: (res) => {
          console.log('成功的', res);
          // res[0].Row_Status = "1"
          console.log('xiugaizzhi', res[0].Row_Status = "1");
          let A = new Array()  //创建一个数组A
          A.push(res[0])    //将form表单添加到A中
          console.log(A)
          let B = new Object() //创建一个对象B
          B.grid_information = A  //将A赋给B的grid_information（名字可以自定义）属性
          console.log(B)
          let C = new Object()  //创建一个对象C
          C.Modify = B             //将B赋给C的Add属性
          console.log('删除的当大', C)
          $.ajax({
            headers: {
              "content-type": "application/x-www-form-urlencoded",
            },
            url: "https://service.essp.top/apiForMoHe/generalBusiness/generalSave",
            method: "post",
            data: {
              Data: JSON.stringify(C),
            },
            success: (res) => {
              console.log(res);
              console.log('uu', res);
            },
            error: (err) => {
              console.log(err);
            },
          });

        },
        error: (err) => {
          console.log(err);
        },
      });





    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      // 在这里可以根据新的 pageSize 重新获取数据
    },
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.postgetList(newPage);
      // 在这里可以根据新的 currentPage 重新获取数据
    },
    //树的数据
    Treedata() {
      const _this = this;
      $.ajax({
        url: `${mh}/clientOperation/getDistrictTree`,
        type: "GET",
        data: {
          DistrictCode: '232723'

        },
        success: (data) => {
          console.log(data);
          this.treeData = data; // 直接使用 this
        },
        dataType: "json",
      });
    },
    //获取表格的列
    getList() {
      const _this = this;
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/generalBusiness/generalQueryAllColumnByTableName`,
        method: "get",
        data: {
          Data: "grid_patrol_task",
        },
        success: (res) => {
          console.log(res);
          sessionStorage.setItem("TABLE_NAME", res[0].TABLE_NAME);
          let PRI = res.filter((item) => {
            return item.COLUMN_KEY === "PRI";
          });
          if (res.length > 0) {
            let key = PRI[0].COLUMN_NAME;
            console.log(key);
            sessionStorage.setItem("key", key);
          }
          let data = res.filter((item) => {
            return item.COLUMN_COMMENT;
          });
          console.log(data);

          let Rdata = data.filter((item) => item.COLUMN_NAME !== "Row_Status");
          console.log(Rdata);
          _this.columns = Rdata;
          _this.checkedFields = Rdata;
          _this.fields = Rdata;
          _this.isDataReady.value = true;
          _this.loading = true;
          _this.postgetList(_this.currentPage);
        },
        error: (err) => {
          console.log(err);
        },
      });
    },
    //获取表格的数据
    postgetList() {
      let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
      console.log(TABLE_NAME);
      const _this = this;
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/generalBusiness/generalPaginateQueryByTableName`,
        method: "post",
        data: {
          Data: TABLE_NAME,
          Condition: "Row_Status='0'",
          PageNumber: 1, //页码
          RowAmount: 10,
        },
        success: (res) => {
          console.log(111);
          console.log(res);
          _this.Total = res.Total;
          _this.loading = false;
          _this.Tabletdata = res.Rows;
          _this.originalTableData = res.Rows; // 保存原始数据
        },
        error: (err) => {
          console.log(err);
        },
      });
    },

    handleSelectionChange() { },
    //查询按钮
    check() {
      console.log(this.input);
      console.log(this.value);
      // 1. 过滤树数据
      if (this.input) {
        this.treeData = this.filterTreeData(this.originalTreeData, this.input);
      } else {
        this.treeData = this.originalTreeData;
      }
      // 2. 过滤表格数据
      let filteredData = [...this.originalTableData];
      // 根据下拉框的值过滤 Political_Affiliation
      if (this.value) {
        filteredData = filteredData.filter(
          (item) => item.Political_Affiliation === this.value
        );
      }
      this.Tabletdata = filteredData;
      console.log("查询操作");
    },
    //重置按钮
    reset() {
      // 这里可以根据你的实际需求修改逻辑
      window.location.reload();
      console.log("重置操作");
    },
    //过滤树的数据
    filterTreeData(data, searchText) {
      return data.filter((item) => {
        if (item.label.includes(searchText)) {
          return true;
        }
        if (item.children) {
          const filteredChildren = this.filterTreeData(
            item.children,
            searchText
          );
          if (filteredChildren.length > 0) {
            item.children = filteredChildren;
            return true;
          }
        }
        return false;
      });
    },
    //添加按钮
    personalAdds(arg_nodekey) {
      console.log('arg_nodekey', arg_nodekey.ProvinceNumber);
      window.location.href = "./TreedataAdd.html?nodeKey=" + arg_nodekey.ProvinceNumber + "&action=Add";
    },
    //编辑按钮
    handleEdits(arg_nodekey) {
      console.log('arg_nodekey', arg_nodekey);
      window.location.href = "./TreedataAdd.html?nodeKey=" + arg_nodekey.ProvinceNumber + "&action=Modify";
    },
    //添加按钮
    personalAdd() {
      const selectedNodes = this.$refs.tree.getCheckedNodes();

      window.location.href = "./grid_patrol_taskAdd.html?nodeKey=" + selectedNodes[0].ProvinceNumber + "&action=Add";
    },
    //编辑按钮
    handleEdit(index, row) {
      console.log(index);
      console.log('row', row);
      // 定义一个变量theValueOfTheTableName让她获取当前数据的选中的值,然后通过url查询后,编辑条件的时候传过去,查找到当前对应的编辑表格的数据
      let theValueOfTheTableName = row.Grid_SerialNumber
      console.log('theValueOfTheTableName', theValueOfTheTableName);


      window.location.href = "./grid_patrol_taskAdd.html?nodeKey=" + theValueOfTheTableName + "&action=Modify";
    },
    // 弹窗全选
    handleAllChecked(val) {
      console.log(val);
      console.log(val);
      let a; // 提前声明变量 a
      if (val === false) {
        console.log(this.fields);
        a = this.fields.filter((item) => {
          return item.COLUMN_KEY == "PRI";
        });
        console.log(a);
      } else {
        a = [...this.fields]; // 当 val 为 true 时，给 a 赋值为 props.fields 的副本
      }
      // 使用 emit 更新 checkedFields
      this.checkedFields = a;
    },
    saveFields() {
      const selectedFields = this.fields.filter((item) => {
        return this.checkedFields.some(
          (cItem) => cItem && cItem.COLUMN_NAME === item.COLUMN_NAME
        );
      });
      console.log("选中的字段:", selectedFields);
      // 更新 columns
      this.columns = selectedFields;
      this.showDialog = false;
    },
    //删除数据
    handleDelete(index, row) {
      let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
      console.log(TABLE_NAME);
      console.log(index, row);
      row.Row_Status = "1";
      let lArray = new Array();
      lArray.push(row);
      let lJSONObject = new Object();
      lJSONObject[TABLE_NAME] = lArray; //换表
      let lJSONObject_DataSet = new Object(); // Modify
      lJSONObject_DataSet.Modify = lJSONObject; //换表
      const _this = this;
      // return;
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/clientOperation/generalSaveForClient`,
        method: "post",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: (res) => {
          console.log(res);
          _this.postgetList();
        },
        error: (err) => {
          console.log(err);
        },
      });
    },
    //树的点击事件
    handleNodeClick(data) {
      console.log(data);
    },
    //显示字段弹窗
    handleOpenDialog() {
      this.showDialog = true;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
  },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus);
app.mount("#app");
